package com.ybkj.daijia.server.controller;

import com.ybkj.daijia.server.exception.NoPermissionException;
import com.ybkj.daijia.page.Page;
import com.ybkj.daijia.server.model.Brand;
import com.ybkj.daijia.server.model.BrandType;
import com.ybkj.daijia.server.service.BrandService;
import com.ybkj.daijia.server.service.BrandTypeService;
import com.ybkj.daijia.server.service.CarService;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.subject.Subject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

/**
 * @author Shine
 */
@Controller
@RequestMapping(value = "brand")
public class BrandController {

    @Autowired
    private BrandService brandService;

    @Autowired
    private BrandTypeService brandTypeService;

    @Autowired
    private CarService carService;

    @RequestMapping(value = {"list", "/"}, method = RequestMethod.GET)
    public ModelAndView list() {
        Subject subject = SecurityUtils.getSubject();
        if (subject.isPermitted("brand")) {

            Page<Brand> page = brandService.queryAll();
            ModelAndView mv = new ModelAndView();
            mv.addObject("page", page);
            mv.setViewName("brand/list");
            return mv;
        } else {
            throw new NoPermissionException();
        }
    }

    @RequestMapping(value = "toAdd", method = RequestMethod.GET)
    public ModelAndView add(HttpSession session) {
        ModelAndView mv = new ModelAndView();
        mv.setViewName("brand/add");
        return mv;
    }

    @RequestMapping(value = "add", method = RequestMethod.POST)
    public String create(@RequestParam("name") String name,
        @RequestParam("typeNames") String[] typeNames,
        @RequestParam("sequence") Integer sequence,
        @RequestParam("photo") String photo, HttpServletRequest req,
        Model model) {

        Subject subject = SecurityUtils.getSubject();
        if (subject.isPermitted("brand")) {

            for (int i = 0; i < typeNames.length; i++) {
                for (int j = i + 1; j < typeNames.length; j++) {
                    if (!typeNames[i].equals("") && typeNames[i].equals(typeNames[j])) {
                        model.addAttribute("message", "请输入不同型号名称!");
                        model.addAttribute("brandType", new BrandType());
                        return "brand/add";
                    }
                }
            }

            Brand b = brandService.findByName(name);
            if (b != null) {
                model.addAttribute("message", "此品牌已存在!!");
                model.addAttribute("brand", new Brand());
                return "brand/add";
            }

            Brand brand = new Brand();
            brand.setName(name);
            brand.setPhoto(photo);
            brand.setCreated(new Date());
            brand.setUpdated(new Date());
            brand.setSequence(sequence);
            int i = brandService.save(brand);
            if (i != 0) {
                Brand findByName = brandService.findOnlyByName(name);
                Long brandId = findByName.getId();

                for (String s : typeNames) {
                    if (!s.equals("")) {
                        BrandType newBrandType = new BrandType();
                        newBrandType.setName(s);
                        newBrandType.setCreated(new Date());
                        newBrandType.setUpdated(new Date());
                        newBrandType.setBrandId(brandId);
                        int j = brandTypeService.save(newBrandType);
                        if (j != 0) {
                        } else {
                            model.addAttribute("message", "保存品牌型号:" + s
                                + "失败!!!!!!!");
                            return "brand/add";
                        }
                    }
                }
                return "redirect:list";
            } else {
                model.addAttribute("message", "保存失败!!");
                model.addAttribute("brand", new Brand());
                return "brand/add";
            }
        } else {
            throw new NoPermissionException();
        }
    }

    @RequestMapping(value = "edit/{id}", method = RequestMethod.GET)
    public ModelAndView toEdit(@PathVariable("id") Long id) {
        ModelAndView mv = new ModelAndView();
        Brand brand = brandService.findById(id);

        mv.addObject("brand", brand);

        mv.setViewName("brand/edit");
        return mv;
    }

    @RequestMapping(value = "update", method = RequestMethod.POST)
    public String update(Brand brand, Model model) {

        Subject subject = SecurityUtils.getSubject();
        if (subject.isPermitted("brand")) {
            List<Brand> list = brandService.findAll();
            for (Brand b : list) {
                if (brand.getName().equals(b.getName())) {
                    if (!b.getId().equals(brand.getId())) {
                        model.addAttribute("message", "此品牌已经存在!");
                        model.addAttribute("brand", brand);
                        return "brand/edit";
                    }
                }
            }

            brand.setName(brand.getName());
            brand.setSequence(brand.getSequence());
            brand.setPhoto(brand.getPhoto());
            brand.setUpdated(new Date());

            int i = brandService.update(brand);
            if (i != 0) {
            }
            return "redirect:list";
        } else {
            throw new NoPermissionException();
        }

    }

    @RequestMapping(value = "delete", method = RequestMethod.POST)
    public @ResponseBody
    Map<String, Object> delete(Long id, HttpServletRequest req) {
        Subject subject = SecurityUtils.getSubject();
        if (subject.isPermitted("brand")) {
            Map<String, Object> result = new HashMap<String, Object>();
            int i = brandService.delete(id, req);
            if (i != 0) {
                result.put("success", true);
            } else {
                result.put("success", false);
                result.put("massage", "品牌删除失败");
            }
            return result;
        } else {
            throw new NoPermissionException();
        }

    }
}
